package com.ae.shield.common.items.shieldFittings.makers;

import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.energy.IEnergyStorage;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

public class NanoRobot extends ShieldMakerBase {
    protected int effectsCount = 4;

    public NanoRobot() {
        super(new Properties(), new ShieldProperty()
                .recovery(0.075f)
                .cd(1)
                .maxIntensity(32f)
                .close(1f)
                .remote(1f)
                .magic(0.6f)
                .other(0.8f)
                .upperLimit(30f)
                .floorLimit(0f)
                .maxPiles(2)
                .penalty(10f)
                .cost(10));
    }

    @Override
    public void addCtrlInformation(List<ITextComponent> components, ItemStack stack) {
        for (int i = 0; i < effectsCount; i++) {
            EffectInstance instance = readEffect(stack, i);
            if (instance != null) {
                ITextComponent effect = ((TranslationTextComponent)instance.getPotion().getDisplayName()).mergeStyle(TextFormatting.BLUE);
                ITextComponent level = new StringTextComponent(String.valueOf(instance.getAmplifier() + 1)).mergeStyle(TextFormatting.BLUE);
                components.add(new TranslationTextComponent("tooltip.ae.shield.effect", effect, level).mergeStyle(TextFormatting.GRAY));
            }
        }
        components.add(new TranslationTextComponent("tooltip.ae.shield.nano_robot.tip1").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.nano_robot.tip2").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.nano_robot.tip3").mergeStyle(TextFormatting.GRAY));
    }

    @Override
    public void shieldTick(@Nonnull ItemStack stack, @Nonnull World world, @Nonnull Entity entity, IEnergyStorage energyCap) {
        effectWork(stack, entity, energyCap);
    }

    public void effectWork(ItemStack stack, Entity entity, IEnergyStorage energyCap) {
        if (getIntensity(stack) <= 0 && getPlies(stack) <= 0) {
            return;
        }

        for (int i = 0; i < effectsCount; i++) {
            LivingEntity living = (LivingEntity) entity;
            EffectInstance effect = readEffect(stack, i);

            if (effect == null) {
                continue;
            }

            if (living.isPotionActive(effect.getPotion())) {
                continue;
            }

            if (energyCap.extractEnergy(50, true) < 50) {
                break;
            }

            energyCap.extractEnergy(50, false);
            living.addPotionEffect(effect);
        }
    }

    public void writeEffect(ItemStack stack, CompoundNBT effect, int index) {
        if (index > effectsCount) {
            throw new IllegalArgumentException("错误的索引，输入的index：" + index + "  index范围：0至" + (effectsCount - 1));
        }

        if (stack.getOrCreateTag().contains("Effects")) {
            ListNBT effects = stack.getOrCreateTag().getList("Effects", 10);
            effects.set(index, effect);
        } else {
            ListNBT effects = new ListNBT();
            for (int i = 0; i < effectsCount; i++) {
                if (i == index) {
                    effects.add(i, effect);
                } else {
                    effects.add(i, new CompoundNBT());
                }
            }
            stack.getOrCreateTag().put("Effects", effects);
        }
    }

    @Nullable
    public EffectInstance readEffect(ItemStack stack, int index) {
        if (index > effectsCount) {
            throw new IllegalArgumentException("错误的索引，输入的index：" + index + "  index范围：0至" + effectsCount);
        }

        if (stack.getOrCreateTag().contains("Effects")) {
            CompoundNBT effect = (CompoundNBT) stack.getOrCreateTag().getList("Effects", 10).get(index);
            if (effect.contains("Id")) {
                return EffectInstance.read(effect);
            }
        }
        return null;
    }
}
